package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.InfoMapper;
import com.stylefeng.guns.common.persistence.model.Info;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.modular.system.dao.OrgInfoDao;
import com.stylefeng.guns.modular.system.warpper.InfoWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 投诉信息控制器
 *
 * @author fengshuonan
 * @Date 2018-10-23 10:29:11
 */
@Controller
@RequestMapping("/info")
public class InfoController extends BaseController {

    private String PREFIX = "/system/info/";


    @Autowired
    InfoMapper infoMapper;
    @Autowired
    OrgInfoDao orgInfoDao;
    @Resource
    private GunsProperties gunsProperties;
    /**
     * 跳转到投诉信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "info.html";
    }

    /**
     * 跳转到添加机构信息
     */
    @RequestMapping("/info_add")
    public String infoAdd() {
        return PREFIX + "info_add.html";
    }

    /**
     * 跳转到修改机构信息
     */
    @RequestMapping("/info_update/{infoId}")
    public String infoUpdate(@PathVariable Integer infoId, Model model) {
        Info old = this.infoMapper.selectById(infoId);
        model.addAttribute("info",old);
        return PREFIX + "info_edit.html";
    }

    /**
     * 获取机构信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> infos = orgInfoDao.selectByCurrentState2();
        Object warp = new InfoWarpper(infos).warp();
        return warp;
    }
    /**
     * 新增机构信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public String upload(@RequestPart("file") MultipartFile picture) {
        //设置图片为唯一的uuid
        String pictureName = UUID.randomUUID().toString() + ".jpg";
        try {
            //获取上传路径
            String fileSavePath = gunsProperties.getFileUploadPath();
            /**
             * transferTo在开发Web应用程序时比较常见的功能之一，
             * 就是允许用户利用multipart请求将本地文件上传到服务器,
             * Spring通过对ServletAPI的HttpServletRequest接口进行扩展，使其能够很好地处理文件上传
             */
            picture.transferTo(new File(fileSavePath + pictureName));
        } catch (Exception e) {
            throw new BussinessException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return pictureName;
    }
    /**
     * 删除机构信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
     @BussinessLog(value = "删除投诉信息",key = "infoId",dict = Dict.DeleteDict)
    public Object delete(@RequestParam long infoId) {
        this.infoMapper.deleteById(infoId);
        return SUCCESS_TIP;
    }
    /**
     * 修改节点
     */
//    @RequestMapping(value = "/update")
//    @ResponseBody
//    @BussinessLog(value = "修改节点",key = "infoId",dict = Dict.DictMap)
//    public Object up(Info info) {
//        Integer update=infoMapper.updateById(info);
//        return super.SUCCESS_TIP;
//    }

    /**
     * 修改机构信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改结构",key = "infoId",dict = Dict.DictMap)
    public Object update(Info info) {
        Integer update = infoMapper.updateById(info);
        return super.SUCCESS_TIP;
    }
    /**
     * 机构信息详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
